package com.zhuge_new.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.FadeExit.FadeExit;
import com.flyco.animation.FlipEnter.FlipHorizontalEnter;
import com.flyco.animation.FlipEnter.FlipVerticalSwingEnter;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.MaterialDialog;
import com.flyco.dialog.widget.NormalDialog;
import com.jungly.gridpasswordview.GridPasswordView;
import com.jungly.gridpasswordview.PasswordType;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.maning.mndialoglibrary.MToast;
import com.uuzuche.lib_zxing.view.ViewfinderView;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.Dialog.Hiht_Dialog;
import com.zhuge_new.Dialog.PassWor_Dialog;
import com.zhuge_new.R;
import com.zhuge_new.bean.ChuxuBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.NumberUtil;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.SoftKeyboardUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by lenovo on 2018-09-17.
 */

public class PutForwordActivity extends BaseActivity implements View.OnClickListener {

    private EditText put_etmonry;
    private TextView put_residue;
    private TextView put_all;
    private String moneyWithdraw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_putforword);
        init_title();
        getprofits();
        hiht_dialog();
    }

    @Override
    public void init_title( ) {
        super.init_title();
        TextView tv_title = findViewById(R.id.tv_nav_title);
        tv_title.setText("我要提现");
        ImageView iv_back = findViewById(R.id.iv_nav_back);
        put_etmonry = findViewById(R.id.put_etmonry);
        Button btn_put = findViewById(R.id.btn_put);
        put_residue = findViewById(R.id.put_residue);
        put_all = findViewById(R.id.put_all);
        TextView hiht_dialog=findViewById(R.id.hiht_dialog);
        btn_put.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        put_all.setOnClickListener(this);
        hiht_dialog.setOnClickListener(this);
        String isPass=PreferencesUtils.getString(baseContext,"isPass");
        String savings=PreferencesUtils.getString(baseContext,"savings");
        if (isPass.equals("1")){
            if (savings.equals("false")){
                chuxuDialog();
            }
        }else if (isPass.equals("0")){
            MToast.makeTextShort(baseContext,"实名认证正在审核，请稍后再试").show();
        }else if (isPass.equals("-1")){
            realname_dialog();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(PutForwordActivity.class);
                break;
            case R.id.put_all:
                put_etmonry.setText(moneyWithdraw);
                break;
            case R.id.btn_put:
                if (Double.valueOf(put_etmonry.getText().toString())<2){
                    MToast.makeTextShort(baseContext,"输入金额不能小于两元").show();
                }else if (Double.valueOf(put_etmonry.getText().toString())>Double.valueOf(moneyWithdraw)){
                    MToast.makeTextShort(baseContext,"输入金额不能大于可提现金额").show();
                }else {
                    submit();
                }
                break;
            case R.id.hiht_dialog:
                hiht_dialog();
                break;
        }
    }

    private void hiht_dialog() {
        Hiht_Dialog hiht_dialog=new Hiht_Dialog(baseContext);
        hiht_dialog.onCreateView();
        hiht_dialog.setUiBeforShow();
        hiht_dialog.show();
    }

    private void submit() {
        PassWor_Dialog dialog=new PassWor_Dialog(baseContext) {

            private ImageView withdraw_close;
            private GridPasswordView gpv_pwd;

            @Override
            public View onCreateView() {
                View view=View.inflate(baseContext,R.layout.dialog_member,null);
                gpv_pwd = view.findViewById(R.id.gridview);
                TextView withdraw_hint=view.findViewById(R.id.withdraw_hint);
                withdraw_close = view.findViewById(R.id.withdraw_close);
                TextView withdraw_title= view.findViewById(R.id.withdraw_title);
                withdraw_hint.setTextSize(22f);
                withdraw_title.setText("确认提现");
                withdraw_hint.setText("请输入交易密码");
                gpv_pwd.setPasswordType(PasswordType.NUMBER);
                return view;
            }

            @Override
            public void setUiBeforShow() {
                withdraw_close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });
                gpv_pwd.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
                    @Override
                    public void onTextChanged(String psw) {
                    }

                    @Override
                    public void onInputFinish(String psw) {
                        OkGo.<String>post(BaseHttp.paypawd_check)
                                .tag(PutForwordActivity.this)
                                .headers("token",PreferencesUtils.getString(baseContext,"token"))
                                .params("paypawd",psw)
                                .execute(new StringDialogCallback(baseContext) {
                                    @Override
                                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                                        if (msgCode.equals("100")){
                                            getend();
                                            dismiss();
                                        }
                                    }
                                });
                    }
                });
            }
        };
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                SoftKeyboardUtil.showSoftInput(PutForwordActivity.this);
            }
        });
        dialog.show();
    }

    private void chuxuDialog() {
        LinearLayout activity_putfoword=findViewById(R.id.activity_putfoword);
        activity_putfoword.setVisibility(View.GONE);
        BaseAnimatorSet bas_in = new FlipHorizontalEnter();
        BaseAnimatorSet bas_out = new FadeExit();
        final NormalDialog dialog = new NormalDialog(baseContext);
        dialog.content("没有绑定储蓄卡，无法进行此业务")
                .style(NormalDialog.STYLE_TWO)
                .titleTextSize(23)
                .showAnim(bas_in)
                .dismissAnim(bas_out)
                .show();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        Intent intent = new Intent(PutForwordActivity.this, CreditCardActivity.class);
                        startActivity(intent);
                        dialog.dismiss();
                    }
                });
    }

    public void getprofits() {
        OkGo.<String>post(BaseHttp.get_profits)
                .tag(this)
                .params("openId",PreferencesUtils.getString(baseContext,"unionid"))
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj=new JSONObject(response.body()).getJSONObject("object");
                            moneyWithdraw = obj.getString("moneyWithdraw");
                            moneyWithdraw= NumberUtil.saveRound(moneyWithdraw,2);
                            put_residue.setText("可提现金额"+moneyWithdraw+"元");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void getend() {
        String money=put_etmonry.getText().toString();
        OkGo.<String>post(BaseHttp.wx_withdraw)
                .tag(this)
                .headers("token",PreferencesUtils.getString(baseContext,"token"))
                .params("openid",PreferencesUtils.getString(baseContext,"unionid"))
                .params("money",money)
                .params("loginType","app")
                .params("withdrawMethod","app")
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                       MToast.makeTextShort(baseContext,msg).show();
                        getprofits();
                        put_etmonry.getText().clear();
                    }
                });
    }
    //实名认证对话框
    private void realname_dialog() {
        BaseAnimatorSet bas_in = new FlipVerticalSwingEnter();
        BaseAnimatorSet bas_out = new FadeExit();
        final MaterialDialog dialog = new MaterialDialog(baseContext);
        dialog.btnNum(1)
                .content("您还没有进行实名认证，是否现在去认证？")//
                .btnText("去认证")//
                .showAnim(bas_in)//
                .dismissAnim(bas_out)//
                .show();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        Intent intent=new Intent(baseContext,RealNameActivity.class);
                        startActivity(intent);
                        dialog.dismiss();
                    }
                });
    }

}
